﻿<!--#include file="conn.asp"-->
<%
dim fname
  fname = now()
  fname = replace(fname,"-","")
  fname = replace(fname," ","") 
  fname = replace(fname,":","")
  fname = replace(fname,"PM","")
  fname = replace(fname,"AM","")
  fname = replace(fname,"上午","")
  fname = replace(fname,"下午","")
  fname = replace(fname,"/","")
  fname = int(fname)
%><html>
<head>
    <title>数据库的备份与恢复</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <link href="images/style.css" type="text/css" rel="stylesheet">
</head>
<body>
    <center>
        <table width="98%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
            <form method="post" name="myform">
            <input name="act" type="hidden" id="act_backup" value="backup">
            <tr>
                <td bgcolor="#FFFFFF">
                    备份文件名：
                    <input type="hidden" name="databasename" value="<%=data%>">
                    <input name="bak_file" type="text" value="<%=request.ServerVariables("APPL_PHYSICAL_PATH")%>sql_bak\<%=data%>_<%=fname%>.bak"
                        size="50">
                    (备份的文件路径)
                </td>
            </tr>
            <tr>
                <td height="30" bgcolor="#FFFFFF">
                    <input name="submit" type="submit" class="Submit" value="确定备份">
                </td>
            </tr>
            </form>
            <tr>
                <td height="30" bgcolor="#FFFFFF">
                    <%
'SQL Server 数据库的备份与恢复!
sqlserver = ""&sqls&"" 'sql服务器
sqlname = ""&datauser&""   '用户名
sqlpassword = ""&datapass&""  '密码
sqlLoginTimeout = 15 '登陆超时
databasename = trim(request("databasename"))
bak_file = trim(request("bak_file"))
act = lcase(request("act"))
datepath = request.ServerVariables("APPL_PHYSICAL_PATH")

if databasename = "" then
 response.write "请按确定进行数据库备份"
else
 if act = "backup" then
  Set srv=Server.CreateObject("SQLDMO.SQLServer")
  srv.LoginTimeout = sqlLoginTimeout
  srv.Connect sqlserver,sqlname, sqlpassword
  Set bak = Server.CreateObject("SQLDMO.Backup")
  bak.Database=databasename
  bak.Devices=Files
  bak.Files=bak_file
  bak.SQLBackup srv
  if err.number>0 then
    response.write err.number&"<font color=red><br>"
    response.write err.description&"</font>"
  end if
  Response.write "<font color=red>数据库备份成功!时间："&now()&" 备份文件名:"&bak_file&"</font>"
 elseif act = "restore" then
  '恢复时要在没有使用数据库时进行！
  Set srv=Server.CreateObject("SQLDMO.SQLServer")
  srv.LoginTimeout = sqlLoginTimeout
  srv.Connect sqlserver,sqlname, sqlpassword
  Set rest=Server.CreateObject("SQLDMO.Restore")
  rest.Action=0 ' full db restore
  rest.Database=databasename
  rest.Devices=Files
  rest.Files=bak_file
  rest.ReplaceDatabase=True 'Force restore over existing database
  if err.number>0 then
    response.write err.number&"<font color=red><br>"
    response.write err.description&"</font>"
  end if
  rest.SQLRestore srv
  
  Response.write "<font color=green>恢复成功!</font>"
 else
  Response.write "<font color=red>没有选择操作</font>"
 end if
end if
                    %>
                </td>
            </tr>
        </table>
    </center>
</body>
</html>
